// pages/user/active.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: '',
    nickName: '',
    gender: '',
    communityId: '',
    birthday: '1950-01-01',
    inputForm: false,
    userInfo: {},
    genderList: ['男','女'],
    communityList: [],
    gindex: 0,
    cindex: 0,
    openid: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let appUserInfo = app.globalData.userInfo
    let openId = app.globalData.activeOpenid
    this.setData({userInfo: appUserInfo, openid: openId})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  onInputMobile: function(e){
    this.setData({ 'userInfo.mobile': e.detail.value })
  },

  onInputIdNo: function(e){
    this.setData({ 'userInfo.idNo': e.detail.value })
  },

  onInputNickName: function(e){
    this.setData({ 'userInfo.nickName': e.detail.value })
  },

  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      birthday: e.detail.value,
      'userInfo.birthday': e.detail.value,
    })
  },

  bindGenderChange: function(e) {
    let idx = e.detail.value
    this.setData({
      gindex: idx,
      'userInfo.gender': this.data.genderList[idx], 
    })
  },

  bindCommunityChange: function(e) {
    let idx = e.detail.value
    let cid = this.data.communityList[idx].communityId
    let cname = this.data.communityList[idx].communityName
    this.setData({
      gindex: idx,
      'userInfo.communityId': cid,
      'userInfo.communityName': cname
    })
  },

  loadAllCommunity: function(){
    let that = this
    let req = this.data
    wx.request({
      url: app.globalData.apiBaseUrl+'/community/all',
      method: 'GET',
      data: req,
      success(res){
        that.setData({
          communityList: res.data.data, 
          'userInfo.communityId': res.data.data[0].communityId,
          'userInfo.communityName': res.data.data[0].communityName
        })
      }
    })
  },

  //激活事件
  submitForm: function(){
    let req = this.data
    wx.request({
      url: app.globalData.apiBaseUrl+'/user/active',
      method: 'POST',
      data: req,
      success(res){
        wx.hideLoading()
        //回调函数
        console.log("激活用户响应:",res)
        if(res.data.code!="0"){
          //激活失败
          wx.showModal({
            title: '提示',
            content: '激活失败',
            showCancel: false,
            complete: (res) => {}
          })
        }else{
          //激活成功
          wx.setStorageSync("TOKEN", res.data.data.token)
          //data: res.data.data.openid
          wx.setStorageSync("LOGIN_USER", res.data.data.user)
          app.globalData.userInfo=res.data.data.user
          let currTime = (new Date()).getTime()
          wx.setStorageSync('LAST_LOGIN_TIME', currTime)
          wx.setStorageSync('CURR_USER_ROLE', res.data.data.user.role)
          wx.showModal({
            title: '提示',
            content: '账号已激活成功',
            showCancel: false,
            complete: (res) => {
              if (res.confirm) {
                wx.switchTab({
                  url: '/pages/service/service-list',
                })
              }
            }
          })
        }
      },
      fail(res){
        console.log("激活失败，重试！",res)
        wx.hideLoading()
      }
    })
  },

  /**
   * 激活老人用户表单
   */
  activeElder: function(){
    this.loadAllCommunity()
    this.setData({
      inputForm: true,
      'userInfo.role': 1,
      'userInfo.status': 1,
      'userInfo.userId': this.data.userInfo.userId
    })
  },

  /**
   * 激活志愿者表单
   */
  activeVol: function(){
    this.setData({
      inputForm: true,
      'userInfo.role': 2,
      'userInfo.status': 2,
      'userInfo.userId': this.data.userInfo.userId
    })
  },

})